package cn.wei.sdd.core.controller.admin;


import cn.wei.common.result.R;
import cn.wei.sdd.base.util.JwtUtils;
import cn.wei.sdd.core.pojo.entity.Notice;
import cn.wei.sdd.core.pojo.vo.ConfirmOrdersVO;
import cn.wei.sdd.core.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Wei
 * @since 2022-02-19
 */
@RestController
@Api(tags = "通知管理")
@RequestMapping("/admin/core/notice")
public class AdminNoticeController {

    @Resource
    private NoticeService noticeService;

    @ApiOperation("通知列表")
    @GetMapping("/getNoticeList")
    public R getNoticeList(){
        List<Notice> noticeList = noticeService.list();
        return R.ok().data("noticeList",noticeList);
    }

    @ApiOperation("修改通知")
    @PostMapping("/updateNoticeById")
    public R updateNoticeById(
            @RequestBody
            @ApiParam(value = "通知对象",required = true) Notice notice,
            HttpServletRequest request){

        String token = request.getHeader("token");
        String userName = JwtUtils.getUserName(token);
        notice.setOperator(userName);
        System.out.println(notice);
        noticeService.updateById(notice);
        return R.ok().message("修改成功");
    }
    @ApiOperation("上下架通知")
    @PutMapping("/lockNoticeById/{id}/{status}")
    public R lockNoticeById(
            @ApiParam(value = "id",required = true)
            @PathVariable(value = "id") Long id,
            @ApiParam(value = "状态0下架，1上架",required = true)
            @PathVariable(value = "status") Integer status,
            HttpServletRequest request){

        String token = request.getHeader("token");
        String userName = JwtUtils.getUserName(token);
        Notice notice = new Notice();
        notice.setId(id);
        notice.setStatus(status);
        notice.setOperator(userName);
        noticeService.updateById(notice);
        return R.ok().message("修改成功");
    }


}

